package com.android.qjpmc.data_module;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSONObject;
import com.android.qjpmc.application_module.home.fragment.AlarmRecordslFragment;
import com.android.qjpmc.application_module.home.fragment.HistoryPageFragment;
import com.android.qjpmc.application_module.home.fragment.StationListFragment;
import com.android.qjpmc.application_module.home.fragment.ViewEventFragment;
import com.android.qjpmc.application_module.home.model.StationDataModel;
import com.android.qjpmc.bean.GroupInfoPointDataBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.SiteStationBean;
import com.android.qjpmc.constant.Constant;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Function;

class CComControl {
    final static int STEP_LOAD_STATION_NULL = 0;
    final static int STEP_LOAD_STATION_LIST = 1;
    final static int STEP_LOAD_DEVICE_LIST = 2;
    final static int STEP_LOAD_DATA_POINT_LIST = 3;//加载数据点
    final static int STEP_LOAD_REALTIME_DATA = 4;


    int m_ToLoadStation = 0;
    int m_StationListLoaded = 0;
    int m_ToLoadDevicPara = 0;
    int m_DeviceParaLoaded = 0;
    int m_ToLoadRtData = 0;
    int m_ToLoadValuePara = 0;
    int m_DataPointParaLoaded = 0;
    int m_nStep;
    int m_nToLoad = 0;


    List<PwrDevice> m_RealtimeDeviceList = null;//new ArrayList<PwrDevice>();
    int m_nRtDataDeviceIdx = 0;

    public void SetRTDataList(List<PwrDevice> list) {
        m_RealtimeDeviceList = list;
    }

    public PwrDevice GetRTDataDevice() {
        if (m_RealtimeDeviceList == null) {
            return null;
        }
        if (m_RealtimeDeviceList.size() == 0) {
            return null;
        }
        if (m_nRtDataDeviceIdx >= m_RealtimeDeviceList.size()) {
            m_nRtDataDeviceIdx = 0;
        }
        return m_RealtimeDeviceList.get(m_nRtDataDeviceIdx);
    }

    public PwrDevice ToNextDevice() {
        if (m_RealtimeDeviceList == null) {
            return null;
        }
        if (m_RealtimeDeviceList.size() == 0) {
            return null;
        }
        m_nRtDataDeviceIdx++;
        if (m_nRtDataDeviceIdx >= m_RealtimeDeviceList.size()) {
            m_nRtDataDeviceIdx = 0;
        }
        return m_RealtimeDeviceList.get(m_nRtDataDeviceIdx);
    }

};

public class ComManager {

    static final int COM_PATROL_MODE_NULL = 0;
    static final int COM_PATROL_MODE_CYCLE = 1; //循环模式
    static final int COM_PATROL_MODE_FOCUS = 2;//聚焦模式

    int m_ComPatrolMode = COM_PATROL_MODE_FOCUS;

    StationDataModel m_ApiProxy = new StationDataModel();
    CComControl m_ComCtrl = new CComControl();
    int m_GetRealtime = Constant.FALSE;
    // handler类接收数据
    Thread m_RtDataThread = null;
    @SuppressLint("HandlerLeak")
    Handler m_handler = new Handler() {
        @SuppressLint("HandlerLeak")
        public void handleMessage(Message msg) {
                        /*
                        if (msg.what == 1) 
                        {

                        }
                        if (msg.what == 2)
                        {

                        }*/
            switch (msg.what) {
                case CComControl.STEP_LOAD_STATION_NULL: {

                }
                break;
                case CComControl.STEP_LOAD_STATION_LIST: {
                    m_ComCtrl.m_nStep = 0;
                    if (m_ComCtrl.m_ToLoadDevicPara == 1) {
                        m_ComCtrl.m_ToLoadDevicPara = 0;
                        LoadStationList();
                    }
                    ;
                }
                break;
                case CComControl.STEP_LOAD_DEVICE_LIST: {
                    m_ComCtrl.m_nStep = 0;
                    m_ComCtrl.m_ToLoadDevicPara = 1;
                    PwrStation Station = PwrDataModule.getInstance().GetFirstStation();
                    Logger.d("StationListFragmentInit --- station22===" + (Station == null));

                    if (Station != null) {
                        //加载站下面所有设备数据
                        getGroupListOfStation(Station);
                        System.out.println("获取设备列表:" + Station.getM_Name());
                    }
                }
                break;
                case CComControl.STEP_LOAD_DATA_POINT_LIST: {
                    GetCurrentDataDeviceDataPoints();
                    System.out.println("获取所有数据点列表:");
                }
                break;
                case CComControl.STEP_LOAD_REALTIME_DATA: {
                    m_ComCtrl.m_nStep = 0;
                }
                break;


            }
            //if (m_ComCtrl != )
        }

        ;
    };

    class ThreadShow implements Runnable {

        final static int SLEEP_SPAN_LOAD_PARA = 20;
        final static int SLEEP_SPAN_LOAD_RT_DATA = 2000;

        int m_SleepSpan = SLEEP_SPAN_LOAD_PARA;

        void SetSleepSpan(int Span) {
            m_SleepSpan = Span;
        }

        @Override
        public void run() {
            LoadStationList();
            while (true) {
                try {
                    Thread.sleep(m_SleepSpan);


                    if (PwrDataModule.getInstance().IsStationDownLoad() == 1) {
//                                                Logger.d("StationListFragmentInit --- IsStationDownLoad===1" );


                        if (m_ComCtrl.m_nToLoad == 1) {
                            m_ComCtrl.m_nToLoad = 0;
                            Message msg = new Message();
                            msg.what = m_ComCtrl.m_nStep;
                            m_handler.sendMessage(msg);
                        }
                        //       Logger.d("StationListFragmentInit --- m_nStep==="+m_ComCtrl.m_nStep );
                        switch (m_ComCtrl.m_nStep) {
                            case CComControl.STEP_LOAD_STATION_NULL: {

                            }
                            break;
                            case CComControl.STEP_LOAD_STATION_LIST: {

                            }
                            break;
                            case CComControl.STEP_LOAD_DEVICE_LIST: {
                            }
                            break;
                            case CComControl.STEP_LOAD_DATA_POINT_LIST: {
                                PwrDevice device = PwrDataModule.getInstance().GetCurrentDataDevice();
                                Logger.d("StationListFragmentInit --- device===" + (device == null));

                                if (device != null) {
                                    Logger.d("StationListFragmentInit --- device===" + (device.IsPointLoaded()));

                                    if (device.IsPointLoaded() == 1) {
                                        device = PwrDataModule.getInstance().ToNextDataDevice();
                                        //当前设备数据点已经加载完成,加载下一个
                                        if (device == null) {
                                            //已经加载完成
                                            System.out.println("数据点已经加载完成");
                                            PwrDataModule.getInstance().LinkDataItemToJXT();
                                            System.out.println("连接数据点到哦接线图");

                                            m_ComCtrl.m_nStep = CComControl.STEP_LOAD_REALTIME_DATA;
                                            //
                                            BuildRealtimePatrolDeviceList();

                                            StationListFragment fragment =
                                                    PwrDataModule.getInstance().GetStationListFragment();
                                            Logger.d("StationListFragmentisNull" + (fragment == null));
                                            if (fragment != null) {
                                                fragment.UpdateStationListUI();
                                                //returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList());
                                            }
                                            AlarmRecordslFragment fragment1 = PwrDataModule.getInstance().GetAlarmRecordsFragment();
                                            if (fragment1 != null) {
                                                fragment1.UpdateStationListUI();
                                                //returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList());
                                            }
                                            HistoryPageFragment fragment2 = PwrDataModule.getInstance().GetHistoryFragment();
                                            if (fragment2 != null) {
                                                fragment2.UpdateStationListUI();
                                                //returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList());
                                            }

                                            ViewEventFragment eventFragment = PwrDataModule.getInstance().GetEventFragment();
                                            if (eventFragment != null) {
                                                eventFragment.UpdateStationListUI();
                                            }
                                            SetSleepSpan(SLEEP_SPAN_LOAD_RT_DATA);

                                        } else {
                                            m_ComCtrl.m_nToLoad = 0;
                                            Message msg = new Message();
                                            msg.what = CComControl.STEP_LOAD_DATA_POINT_LIST;
                                            m_handler.sendMessage(msg);
                                        }
                                    }
                                    ;
                                } else {
                                    //已经加载完成
                                    System.out.println("异常,当前设备为空!");
                                }
                            }
                            break;
                            case CComControl.STEP_LOAD_REALTIME_DATA: {
                                //System.out.println("获取实时数据:");

                                if (m_GetRealtime == Constant.FALSE) {
                                    //CComControl ctrl = GetC
                                    if (m_ComCtrl == null) {
                                        ;
                                    } else {
                                        switch (m_ComPatrolMode) {
                                            case COM_PATROL_MODE_CYCLE: {
                                                //依次获取设备的实时数据
                                                PwrDevice device = m_ComCtrl.ToNextDevice();
                                                m_GetRealtime = Constant.TRUE;
                                                GetRealtimeDataOfDevice(device);
                                            }
                                            break;
                                            case COM_PATROL_MODE_FOCUS: {
                                                RTDataEntrance entrance =
                                                        PwrDataModule.getInstance().m_RtDataEntranceMng.GetCurrentRtDataEntrance();
                                                GetRealtimeDataOfDevice(entrance);
                                            }
                                            break;
                                        }

                                    }
                                } else {
                                    m_GetRealtime++;
                                    if (m_GetRealtime >= 30) {
                                        m_GetRealtime = 0;
                                    }
                                }
                            }
                            break;
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("thread error...");
                }
            }
        }
    }

    public int StartComThread() {
        m_RtDataThread = new Thread(new ThreadShow());
        if (m_RtDataThread != null) {
            m_RtDataThread.start();
        }
        return 0;
    }


    //加载所有站列表
    public void LoadStationList() {
        if (PwrDataModule.getInstance().IsStationDownLoad() == 1) {
            // mView.returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList() );
        }
        m_ApiProxy.getStationList()
                .subscribe(new ProgressObserver<List<SiteStationBean>>() {
                    @Override
                    public void _onNext(List<SiteStationBean> siteStationBeans) {
                        Logger.d("StationListFragmentInit --- station===" + (siteStationBeans.size()));

                        PwrDataModule.getInstance().CreateStationByBeanList(siteStationBeans);
                        PwrDataModule.getInstance().SetStationDownMark(1);
                        m_ComCtrl.m_nToLoad = 1;
                        m_ComCtrl.m_nStep = CComControl.STEP_LOAD_DEVICE_LIST;


                    }

                    @Override
                    public void _onError(String errorMessage, int errorCode) {
                        super._onError(errorMessage, errorCode);
                    }
                });
    }

    //获取站下面所有设备(组)的列表,这里的设备代表电柜
    public void getGroupListOfStation(String stationId) {
        /*
        int nStationID = 0;
        nStationID = Integer.parseInt(stationId);
        int count = PwrDataModule.getInstance().GetStationDeviceCount(nStationID);
        if (count > 0 )
        {
            List<StationGroupBean>  DeviceList = PwrDataModule.getInstance().GetStationDeviceBeanList(nStationID);
            returnStationData(DeviceList);
        }*/
        m_ApiProxy.getGroupListOfStation(stationId).subscribe(new ProgressObserver<List<PwrDeviceBean>>(false, null) {
            @Override
            public void _onNext(List<PwrDeviceBean> pwrDeviceBeans) {
                int nStationID = 0;
                nStationID = Integer.parseInt(stationId);
                PwrStation station = PwrDataModule.getInstance().GetPwrStation(nStationID);
                if (station != null) {
                    station.SetAllDeviceBeLoadedMark(1);
                }
                PwrDataModule.getInstance().CreateAllPowerDeviceOfAStation(nStationID, pwrDeviceBeans);
                Logger.d("IsAllDeviceBeLoaded" + PwrDataModule.getInstance().IsAllDeviceBeLoaded());
                if (PwrDataModule.getInstance().IsAllDeviceBeLoaded() == 1) {
                    if (m_ComCtrl != null) {

                        m_ComCtrl.m_nStep = CComControl.STEP_LOAD_DATA_POINT_LIST;
                        PwrDataModule.getInstance().ToFirstDataDevice();
                        m_ComCtrl.m_nToLoad = 1;
                    }
                }
                // returnStationData(groupOfStationBeans);

            }
        });
    }

    //获取站下面所有设备(组)的列表,这里的设备代表电柜
    public void getGroupListOfStation(PwrStation station) {
        /*
        int nStationID = 0;
        nStationID = Integer.parseInt(stationId);
        int count = PwrDataModule.getInstance().GetStationDeviceCount(nStationID);
        if (count > 0 )
        {
            List<StationGroupBean>  DeviceList = PwrDataModule.getInstance().GetStationDeviceBeanList(nStationID);
            returnStationData(DeviceList);
        }*/
        int stationId = station.getM_ID();
        m_ApiProxy.getGroupListOfStation(station.GetID()).subscribe(new ProgressObserver<List<PwrDeviceBean>>(false, null) {
            @Override
            public void _onNext(List<PwrDeviceBean> pwrDeviceBeans) {

                PwrDataModule.getInstance().CreateAllPowerDeviceOfAStation(stationId, pwrDeviceBeans);
                station.SetAllDeviceBeLoadedMark(1);
                station.CreateDeviceListCntnBean(pwrDeviceBeans);
                Logger.d("IsAllDeviceBeLoaded" + PwrDataModule.getInstance().IsAllDeviceBeLoaded());

                if (PwrDataModule.getInstance().IsAllDeviceBeLoaded() == 1) {
                    //如果最后一个站的最后一个设备已经加载完毕, 进入加载数据点流程
                    if (m_ComCtrl != null) {
                        m_ComCtrl.m_nStep = CComControl.STEP_LOAD_DATA_POINT_LIST;
                        PwrDataModule.getInstance().ToFirstDataDevice();
                        m_ComCtrl.m_nToLoad = 1;
                    }
                }


                // returnStationData(groupOfStationBeans);

            }
        });
    }

    //获取站实时数据
    int GetStationRTData(int StationID) {
        String sStationID;
        PwrStation Station = PwrDataModule.getInstance().GetPwrStationByID(StationID);
        if (Station == null) {
            return 1;
        }
        PwrDevice Dvs = Station.GetFirstDevice();
        while (Dvs != null) {
            List<PointParameterBean> pointList = new ArrayList<PointParameterBean>();
            if (Station.m_station_bean != null) {

                //获取实时数据接口
                GetRealtimeDataOfDevice(Integer.toString(StationID), Dvs.GetDeviceBean(), pointList);
            }
            Dvs = Dvs.GetNext();
        }
        return 1;
    }


    //获取站点的某一设备(数据组)下的点参数
    //stationId: 站ID
    //groupDataBean: 数据组(设备)bean
    public void GetPointDataOfDevice(String stationId, PwrDeviceBean groupDataBean) {
        m_ApiProxy.getPointListOfGroup(groupDataBean.groupId, 1)
                .subscribe(new ProgressObserver<List<PointParameterBean>>(false, null) {
                    @Override
                    public void _onNext(List<PointParameterBean> pointList) {

                        groupDataBean.pointList = pointList; //把获取到的点列表写到设备bean中去
                        //GetRealtimeDataOfDevice(stationId, groupDataBean, pointList);

                    }
                });
    }

    //获取设备的点参数
    public int GetPointDataOfDevice(PwrDevice Device) {
        if (Device == null) {
            return 1;
        }
        m_ApiProxy.getPointListOfGroup(Device.GetDeviceID(), 1)
                .subscribe(new ProgressObserver<List<PointParameterBean>>(false, null) {
                    @Override
                    public void _onNext(List<PointParameterBean> pointList) {
                        PwrDeviceBean bean = Device.GetDeviceBean();
                        if (bean != null) {
                            bean.pointList = pointList; //把获取到的点列表写到设备bean中去
                        }
                        Device.CreateAnalogTeam(pointList);
                        Device.SetPointLoadedMark(1);
/*
                                        if (PwrDataModule.getInstance().IsAllDataPointBeLoaded() == 1)
                                        {
                                                PwrDataModule.getInstance().SetDataPontLoadedMark(1);
                                        }
*/
                        //GetRealtimeDataOfDevice(stationId, groupDataBean, pointList);

                    }
                });
        return 0;
    }

    public int GetCurrentDataDeviceDataPoints() {
        PwrDevice Device = PwrDataModule.getInstance().GetCurrentDataDevice();
        if (Device == null) {
            return 1;
        }
        GetPointDataOfDevice(Device);
        return 0;

    }

    //获取站下面所有的测点数据
    public int GetAllPointDataOfStaion(PwrStation station) {
        if (station == null) {
            return 1;
        }
        PwrDevice device = station.GetFirstDevice();
        while (device != null) {
            GetPointDataOfDevice(device);//获取设备下的参数测点
            device = device.GetNext();
        }
        return 0;
    }

    //获取所有站的测点
    public int GetPointDataOfAllStation() {
        int StationNum = 0;
        PwrStation station = PwrDataModule.getInstance().GetFirstStation();
        while (station != null) {
            StationNum++;
            GetAllPointDataOfStaion(station);
            station = station.GetNext();
        }
        return StationNum;
    }


    //获取站点的实时数据.....
        /*
        stationId: 站id
        groupDataBean:数据组(设备)bean


        *
        * */
    private void GetRealtimeDataOfDevice(String stationId, PwrDeviceBean groupDataBean, List<PointParameterBean> pointList) {
        List<String> pointCodeList = new ArrayList<>();
        for (PointParameterBean groupPointListBean : pointList) {
            pointCodeList.add(StationDataModel.buildPointCode(groupPointListBean));
        }
        m_ApiProxy.getPointDataOfPointCode(stationId, pointCodeList).map(new Function<Object, List<PointParameterBean>>() {
            @Override
            public List<PointParameterBean> apply(Object o) throws Exception {
                String s = JSONObject.toJSONString(o);
                Logger.d("getPointDataOfStation" + s);
                JSONObject jsonObject = JSONObject.parseObject(s);
                Logger.d("getPointDataOfStation22" + Thread.currentThread().getName());
                for (PointParameterBean groupPointListBean : pointList) {
                    Object pointData = jsonObject.get(groupPointListBean.tagnode);
                    GroupInfoPointDataBean groupInfoPointDataBean = JSONObject.parseObject(JSONObject.toJSONString(pointData), GroupInfoPointDataBean.class);
                    groupPointListBean.pointData = groupInfoPointDataBean;
                }
                return pointList;
            }
        }).subscribe(new ProgressObserver<List<PointParameterBean>>(false, null) {
            @Override
            public void _onNext(List<PointParameterBean> o) {

                groupDataBean.isGetPointReamTimeData = true;
                groupDataBean.pointList = pointList;
                                      /*
                                        mView.returnGroupPointList(groupDataBean);

                                       */
            }
        });


    }

    //获取设备实时数据
    private int GetRealtimeDataOfDevice(PwrDevice device) {
        List<PointParameterBean> pointList = null;
        PwrDeviceBean groupDataBean = null;
        if (device == null) {
            return 1;
        }
        PwrDeviceBean device_bean = device.GetDeviceBean();
        if (device_bean == null) {
            return Constant.GEN_ERROR;
        }
        List<String> pointCodeList = new ArrayList<>();
        pointList = device_bean.pointList;
        if (pointList == null) {
            return 1;
        }
        for (PointParameterBean groupPointListBean : pointList) {
            pointCodeList.add(StationDataModel.buildPointCode(groupPointListBean));
        }
        List<PointParameterBean> finalPointList = pointList;
        m_ApiProxy.getPointDataOfPointCode(device_bean.stationId, pointCodeList).map(new Function<Object, List<PointParameterBean>>() {
            @Override
            public List<PointParameterBean> apply(Object o) throws Exception {
                String s = JSONObject.toJSONString(o);
                Logger.d("getPointDataOfStation" + s);
                JSONObject jsonObject = JSONObject.parseObject(s);
                Logger.d("getPointDataOfStation22" + Thread.currentThread().getName());
                Calendar calendar = Calendar.getInstance();
                int index = 0;
                for (PointParameterBean groupPointListBean : finalPointList) {
                    Object pointData = jsonObject.get(groupPointListBean.tagnode);
                    String jasonString = JSONObject.toJSONString(pointData);

                    GroupInfoPointDataBean groupInfoPointDataBean = JSONObject.parseObject(jasonString, GroupInfoPointDataBean.class);
                    //实时数据
                    if (groupInfoPointDataBean.value == null) {
                        //用于调试,把秒数据作为实时数据
                        groupInfoPointDataBean.value = "0.0";//Integer.toString(calendar.get(Calendar.SECOND)+ index*10);
                    } else {
                        //String TheString = pointData.toString();
                        if (pointData.getClass() == JSONObject.class) {
                            JSONObject jsObject = (JSONObject) pointData;
                            String value = jsObject.getString("value");
                            if (groupPointListBean.pointData != null) {
                                groupPointListBean.pointData.value = value;
                            } else {
                                groupPointListBean.pointData = groupInfoPointDataBean;
                            }
                        }
                        Logger.d("Realtime Value:" + groupInfoPointDataBean.value);
                    }


                    index++;
                }
                return finalPointList;
            }
        }).subscribe(new ProgressObserver<List<PointParameterBean>>(false, null) {
            @Override
            public void _onNext(List<PointParameterBean> o) {

                //m_GetRealtime = Constant.FALSE;
                EnableToGetRealtimeData();
                device.UpdateRealtimeValues();
                //groupDataBean.isGetPointReamTimeData = true;
                // groupDataBean.pointList = finalPointList;
                                      /*
                                        mView.returnGroupPointList(groupDataBean);

                                       */
            }
        });
        return 0;
    }


    void EnableToGetRealtimeData() {
        m_GetRealtime = Constant.FALSE;
    }

    //获取实时数据,根据entrance获取.数据点表放在entrance的pointcode里面
    private int GetRealtimeDataOfDevice(RTDataEntrance entrance) {
        PwrDeviceBean groupDataBean = null;
        if (entrance == null) {
            return 1;
        }
        List<PointParameterBean> finalPointList = new ArrayList<PointParameterBean>();
        List<String> pointCodeList = null;
        pointCodeList = entrance.m_PointCodeList;
        if (pointCodeList == null) {
            return 1;
        }
        if (pointCodeList.size() == 0) {
            return Constant.GEN_ERROR;
        }
        m_ApiProxy.getPointDataOfPointCode(entrance.m_StationID, pointCodeList).map(new Function<Object, List<PointParameterBean>>() {
            @Override
            //public List<GroupPointListBean> apply(Object o) throws Exception
            public List<PointParameterBean> apply(Object o) throws Exception {
                String s = JSONObject.toJSONString(o);
                Logger.d("getPointDataOfStation" + s);
                JSONObject jsonObject = JSONObject.parseObject(s);
                Logger.d("getPointDataOfStation22" + Thread.currentThread().getName());
                Calendar calendar = Calendar.getInstance();
                int index = 0;
                //jsonObject.forEach();
                //遍历每个jason对象
                for (Map.Entry entry : jsonObject.entrySet()) {
                    String key = entry.getKey().toString();
                    Object pointData = entry.getValue();
                    if (pointData.getClass() == JSONObject.class) {
                        //把数据写入
                        JSONObject jsObject = (JSONObject) pointData;
                        String value = jsObject.getString("value");
                        entrance.UpdateRTValue(key, value);
                        PointParameterBean bean = new PointParameterBean();
                        finalPointList.add(bean);
                    }
                    //return entry.getValue().toString();
                }


                                /*
                                for (GroupPointListBean groupPointListBean : finalPointList)
                                {
                                        Object pointData = jsonObject.get(groupPointListBean.tagnode);
                                        String jasonString = JSONObject.toJSONString(pointData);

                                        GroupInfoPointDataBean groupInfoPointDataBean = JSONObject.parseObject(jasonString, GroupInfoPointDataBean.class);
                                        //实时数据
                                        if (groupInfoPointDataBean.value == null)
                                        {
                                                //用于调试,把秒数据作为实时数据
                                                groupInfoPointDataBean.value = "0.0";//Integer.toString(calendar.get(Calendar.SECOND)+ index*10);
                                        }
                                        else
                                        {
                                                //String TheString = pointData.toString();
                                                if (pointData.getClass() == JSONObject.class)
                                                {
                                                        JSONObject jsObject = (JSONObject)pointData;
                                                        String value = jsObject.getString("value");
                                                        if (groupPointListBean.pointData != null)
                                                        {
                                                                groupPointListBean.pointData.value = value;
                                                        }
                                                        else
                                                        {
                                                                groupPointListBean.pointData = groupInfoPointDataBean;
                                                        }
                                                }
                                                Logger.d("Realtime Value:" + groupInfoPointDataBean.value);
                                        }


                                        index++;
                                }*/
                return finalPointList;
            }
        }).subscribe(new ProgressObserver<List<PointParameterBean>>(false, null) {
            @Override
            public void _onNext(List<PointParameterBean> o) {
                EnableToGetRealtimeData();
            }
        });
        return 0;
    }

    int BuildRealtimePatrolDeviceList() {
        //m_ComCtrl;
        List<PwrDevice> PatrolList = new ArrayList<PwrDevice>();
        if (PatrolList == null) {
            return Constant.GEN_ERROR;
        }
        PwrDevice device = PwrDataModule.getInstance().ToFirstDataDevice();
        while (device != null) {
            PatrolList.add(device);
            device = PwrDataModule.getInstance().ToNextDataDevice();
        }
        m_ComCtrl.SetRTDataList(PatrolList);
        return Constant.NO_ERROR;
    }


}






















